export default defineEventHandler(async (event) => {
  // 模拟订单数据
  const orders = [
    {
      id: 'ORD-2024-001',
      customer: {
        id: 1,
        name: '张三',
        email: 'zhangsan@example.com',
        phone: '13800138001'
      },
      items: [
        {
          id: 1,
          name: 'MacBook Pro 14英寸',
          price: 14999,
          quantity: 1,
          total: 14999
        }
      ],
      total: 14999,
      status: 'delivered',
      paymentMethod: 'credit_card',
      paymentStatus: 'paid',
      shippingAddress: {
        name: '张三',
        phone: '13800138001',
        address: '北京市朝阳区建国路88号',
        city: '北京',
        postalCode: '100020'
      },
      createdAt: '2024-03-15T10:30:00Z',
      updatedAt: '2024-03-16T14:20:00Z',
      deliveredAt: '2024-03-16T14:20:00Z'
    },
    {
      id: 'ORD-2024-002',
      customer: {
        id: 2,
        name: '李四',
        email: 'lisi@example.com',
        phone: '13800138002'
      },
      items: [
        {
          id: 2,
          name: 'iPhone 15 Pro',
          price: 7999,
          quantity: 1,
          total: 7999
        },
        {
          id: 3,
          name: '机械键盘 Cherry MX',
          price: 599,
          quantity: 1,
          total: 599
        }
      ],
      total: 8598,
      status: 'shipped',
      paymentMethod: 'alipay',
      paymentStatus: 'paid',
      shippingAddress: {
        name: '李四',
        phone: '13800138002',
        address: '上海市浦东新区陆家嘴金融区',
        city: '上海',
        postalCode: '200120'
      },
      createdAt: '2024-03-15T09:15:00Z',
      updatedAt: '2024-03-16T10:30:00Z',
      shippedAt: '2024-03-16T10:30:00Z'
    },
    {
      id: 'ORD-2024-003',
      customer: {
        id: 3,
        name: '王五',
        email: 'wangwu@example.com',
        phone: '13800138003'
      },
      items: [
        {
          id: 3,
          name: '机械键盘 Cherry MX',
          price: 599,
          quantity: 2,
          total: 1198
        }
      ],
      total: 1198,
      status: 'processing',
      paymentMethod: 'wechat_pay',
      paymentStatus: 'paid',
      shippingAddress: {
        name: '王五',
        phone: '13800138003',
        address: '广州市天河区珠江新城',
        city: '广州',
        postalCode: '510623'
      },
      createdAt: '2024-03-15T08:45:00Z',
      updatedAt: '2024-03-15T16:20:00Z'
    },
    {
      id: 'ORD-2024-004',
      customer: {
        id: 4,
        name: '赵六',
        email: 'zhaoliu@example.com',
        phone: '13800138004'
      },
      items: [
        {
          id: 5,
          name: '4K显示器 27英寸',
          price: 2499,
          quantity: 1,
          total: 2499
        }
      ],
      total: 2499,
      status: 'pending',
      paymentMethod: 'credit_card',
      paymentStatus: 'pending',
      shippingAddress: {
        name: '赵六',
        phone: '13800138004',
        address: '深圳市南山区科技园',
        city: '深圳',
        postalCode: '518057'
      },
      createdAt: '2024-03-15T08:20:00Z',
      updatedAt: '2024-03-15T08:20:00Z'
    },
    {
      id: 'ORD-2024-005',
      customer: {
        id: 5,
        name: '钱七',
        email: 'qianqi@example.com',
        phone: '13800138005'
      },
      items: [
        {
          id: 4,
          name: '无线蓝牙耳机',
          price: 299,
          quantity: 1,
          total: 299
        }
      ],
      total: 299,
      status: 'delivered',
      paymentMethod: 'alipay',
      paymentStatus: 'paid',
      shippingAddress: {
        name: '钱七',
        phone: '13800138005',
        address: '杭州市西湖区文三路',
        city: '杭州',
        postalCode: '310012'
      },
      createdAt: '2024-03-14T16:30:00Z',
      updatedAt: '2024-03-15T12:15:00Z',
      deliveredAt: '2024-03-15T12:15:00Z'
    },
    {
      id: 'ORD-2024-006',
      customer: {
        id: 1,
        name: '张三',
        email: 'zhangsan@example.com',
        phone: '13800138001'
      },
      items: [
        {
          id: 2,
          name: 'iPhone 15 Pro',
          price: 7999,
          quantity: 1,
          total: 7999
        }
      ],
      total: 7999,
      status: 'cancelled',
      paymentMethod: 'credit_card',
      paymentStatus: 'refunded',
      shippingAddress: {
        name: '张三',
        phone: '13800138001',
        address: '北京市朝阳区建国路88号',
        city: '北京',
        postalCode: '100020'
      },
      createdAt: '2024-03-14T14:20:00Z',
      updatedAt: '2024-03-15T09:30:00Z',
      cancelledAt: '2024-03-15T09:30:00Z'
    }
  ]

  // 获取查询参数
  const query = getQuery(event)
  const page = parseInt(query.page as string) || 1
  const limit = parseInt(query.limit as string) || 10
  const status = query.status as string
  const customerId = query.customerId as string
  const startDate = query.startDate as string
  const endDate = query.endDate as string
  const search = query.search as string
  const sortBy = query.sortBy as string || 'createdAt'
  const sortOrder = query.sortOrder as string || 'desc'

  // 过滤数据
  let filteredOrders = orders
  if (status) {
    filteredOrders = filteredOrders.filter(order => order.status === status)
  }
  if (customerId) {
    filteredOrders = filteredOrders.filter(order => order.customer.id === parseInt(customerId))
  }
  if (startDate) {
    filteredOrders = filteredOrders.filter(order => order.createdAt >= startDate)
  }
  if (endDate) {
    filteredOrders = filteredOrders.filter(order => order.createdAt <= endDate)
  }
  if (search) {
    filteredOrders = filteredOrders.filter(order => 
      order.id.toLowerCase().includes(search.toLowerCase()) ||
      order.customer.name.toLowerCase().includes(search.toLowerCase()) ||
      order.customer.email.toLowerCase().includes(search.toLowerCase())
    )
  }

  // 排序
  filteredOrders.sort((a, b) => {
    let aValue = a[sortBy as keyof typeof a]
    let bValue = b[sortBy as keyof typeof b]
    
    if (typeof aValue === 'string' && typeof bValue === 'string') {
      return sortOrder === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue)
    }
    
    if (typeof aValue === 'number' && typeof bValue === 'number') {
      return sortOrder === 'asc' ? aValue - bValue : bValue - aValue
    }
    
    return 0
  })

  // 分页
  const startIndex = (page - 1) * limit
  const endIndex = startIndex + limit
  const paginatedOrders = filteredOrders.slice(startIndex, endIndex)

  // 模拟延迟
  await new Promise(resolve => setTimeout(resolve, 10000))

  return {
    code: 200,
    message: '获取订单列表成功',
    data: {
      orders: paginatedOrders,
      pagination: {
        page,
        limit,
        total: filteredOrders.length,
        totalPages: Math.ceil(filteredOrders.length / limit)
      }
    }
  }
}) 